home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / ircii2-6.zip / SRC\IRCII-2.6\SOURCE\FLOOD.C < prev    next >
C/C++ Source or Header  |  1994-12-28  |  3KB  |  133 lines

  1. /*
  2.  * flood.c: handle channel flooding. 
  3.  *
  4.  * This attempts to give you some protection from flooding.  Basically, it keeps
  5.  * track of how far apart (timewise) messages come in from different people.
  6.  * If a single nickname sends more than 3 messages in a row in under a
  7.  * second, this is considered flooding.  It then activates the ON FLOOD with
  8.  * the nickname and type (appropriate for use with IGNORE). 
  9.  *
  10.  * Thanks to Tomi Ollila <f36664r@puukko.hut.fi> for this one. 
  11.  */
  12.  
  13. #ifndef lint
  14. static    char    rcsid[] = "@(#)$Id: flood.c,v 1.5 1994/07/02 02:32:13 mrg Stab $";
  15. #endif
  16.  
  17. #include "irc.h"
  18.  
  19. #include "hook.h"
  20. #include "ircaux.h"
  21. #include "ignore.h"
  22. #include "flood.h"
  23. #include "vars.h"
  24. #include "output.h"
  25.  
  26. static    char    *ignore_types[NUMBER_OF_FLOODS] =
  27. {
  28.     "MSG",
  29.     "PUBLIC",
  30.     "NOTICE",
  31.     "WALL",
  32.     "WALLOP"
  33. };
  34.  
  35. typedef struct flood_stru
  36. {
  37.     char    nick[NICKNAME_LEN + 1];
  38.     int    type;
  39.     char    flood;
  40.     long    cnt;
  41.     time_t    start;
  42. }    Flooding;
  43.  
  44. static    Flooding *flood = (Flooding *) 0;
  45.  
  46. /*
  47.  * check_flooding: This checks for message flooding of the type specified for
  48.  * the given nickname.  This is described above.  This will return 0 if no
  49.  * flooding took place, or flooding is not being monitored from a certain
  50.  * person.  It will return 1 if flooding is being check for someone and an ON
  51.  * FLOOD is activated. 
  52.  */
  53. int    check_flooding(nick, type, line)
  54. char    *nick;
  55. int    type;
  56. char    *line;
  57. {
  58.     static    int    users = 0,
  59.             pos = 0;
  60.     int    i;
  61.     time_t    flood_time,
  62.     diff;
  63.     Flooding *tmp;
  64.  
  65.     if (users != get_int_var(FLOOD_USERS_VAR))
  66.     {
  67.         if ((users = get_int_var(FLOOD_USERS_VAR)) == 0)
  68.             return(1);
  69.         if (flood)
  70.             flood = (Flooding *) new_realloc(flood,
  71.                 sizeof(Flooding) * users);
  72.         else
  73.             flood = (Flooding *) new_malloc(sizeof(Flooding) *
  74.                 users);
  75.         for (i = 0; i < users; i++)
  76.         {
  77.             *(flood[i].nick) = (char) 0;
  78.             flood[i].cnt = 0;
  79.             flood[i].flood = 0;
  80.         }
  81.     }
  82.     if (users == 0)
  83.         return (1);
  84.     for (i = 0; i < users; i++)
  85.     {
  86.         if (*(flood[i].nick))
  87.         {
  88.             if ((my_stricmp(nick, flood[i].nick) == 0) &&
  89.                     (type == flood[i].type))
  90.                 break;
  91.         }
  92.     }
  93.     flood_time = time(0);
  94.     if (i == users)
  95.     {
  96.         tmp = &(flood[pos]);
  97.         pos = (pos + 1) % users;
  98.         strmcpy(tmp->nick, nick, NICKNAME_LEN);
  99.         tmp->type = type;
  100.         tmp->cnt = 1;
  101.         tmp->start = flood_time;
  102.         tmp->flood = 0;
  103.         return (1);
  104.     }
  105.     else
  106.         tmp = &(flood[i]);
  107.     tmp->cnt++;
  108.     diff = flood_time - tmp->start;
  109.     if (tmp->cnt > get_int_var(FLOOD_AFTER_VAR))
  110.     {
  111.         if ((diff == 0) || (tmp->cnt / diff) >
  112.                 get_int_var(FLOOD_RATE_VAR))
  113.         {
  114.             if (tmp->flood == 0)
  115.             {
  116.                 if (get_int_var(FLOOD_WARNING_VAR))
  117.                     say("%s flooding detected from %s",
  118.                         ignore_types[type], nick);
  119.                 tmp->flood = 1;
  120.             }
  121.             return (do_hook(FLOOD_LIST, "%s %s %s", nick,
  122.                 ignore_types[type],line));
  123.         }
  124.         else
  125.         {
  126.             tmp->flood = 0;
  127.             tmp->cnt = 1;
  128.             tmp->start = flood_time;
  129.         }
  130.     }
  131.     return (1);
  132. }
  133.